package vashion.azeroth.common.push.util;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

public class JPushClientUtil {
	
	private static final Logger log = LoggerFactory.getLogger(JPushClientUtil.class);
	  
    //在极光注册上传应用的 appKey 和 masterSecret  
    private static final String appKey ="f746d2c536cf73e0892a4c62";
    private static final String masterSecret = "0cd7ffe2a5718139fc6d3953";
  
  
    public static void main(String[] args) throws Exception {  
        JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance());  
  
        PushPayload payload = buildPush("161a3797c837fe0c1c1", "发一条消息");  
  
        try {  
            PushResult result = jpushClient.sendPush(payload);
            log.info(result.msg_id + "");
            log.info(result.toString());
        } catch (APIConnectionException e) {  
        	log.error("APIConnectionException", e);
        } catch (APIRequestException e) {
        	log.error("APIRequestException", e);  
        	log.info("HTTPStatus: " + e.getStatus());  
        	log.info("ErrorCode: " + e.getErrorCode());  
        	log.info("ErrorMessage: " + e.getErrorMessage());  
        }  
    }
    
    public static void push(String registrationId, String alert) {
    	if(StringUtils.isBlank(registrationId)) {
    		return;
    	}
    	
    	ClientConfig config = ClientConfig.getInstance();
    	config.setTimeToLive(100);
        JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, config);  
  
        PushPayload payload = buildPush(registrationId, alert);  
  
        try {  
            PushResult result = jpushClient.sendPush(payload);
            log.info(result.msg_id + "");
            log.info(result.toString());
        } catch (APIConnectionException e) {  
        	log.error("APIConnectionException", e);
        } catch (APIRequestException e) {
        	log.error("APIRequestException", e);  
        	log.info("HTTPStatus: " + e.getStatus());  
        	log.info("ErrorCode: " + e.getErrorCode());  
        	log.info("ErrorMessage: " + e.getErrorMessage());  
        }  
    }  
  
  
    public static PushPayload buildPush(String registrationId, String alert) {  
        return PushPayload.newBuilder()  
                .setPlatform(Platform.ios())
                .setAudience(Audience.registrationId(registrationId))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(alert)  
                                .setSound("vashionsound.caf")  
//                                .addExtra("issueId", "xxx")  
//                                .addExtra("type","我是问题")  
                                .build())  
                        .build())  
//                .setMessage(Message.content("hgg推车了"))
                .setOptions(Options.newBuilder()  
                        .setApnsProduction(false)  
                        .build())  
                .build();  
    }  
  
}  
